package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.common.fastdfs.FastDFSClient;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmMaterialDto;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmNewsMaterial;
import com.usian.utils.threadlocal.ThreadLocalUtils;
import com.usian.wemedia.mapper.WmmaterialMapper;
import com.usian.wemedia.mapper.WmnewsMaterialMapper;
import com.usian.wemedia.service.WmmaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

@Service
public class WmmaterialServiceImpl extends BaseServiceImpl<WmmaterialMapper, WmMaterial> implements WmmaterialService {
    @Autowired
    FastDFSClient fastDFSClient;
    @Value("${fdfs.url}")
    private String fileServerUrl;
    @Autowired
    private WmnewsMaterialMapper wmnewsMaterialMapper;
    @Override
    public ResponseResult upload(MultipartFile file) {
        if (file==null)return errorParam;
        //上传图片到fastdfs
        String s=null;
        try {
            s = fastDFSClient.uploadFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setUserId(ThreadLocalUtils.getUser());
        wmMaterial.setType((short)0);
        wmMaterial.setUrl(s);
        wmMaterial.setIsCollection((short)0);
        save(wmMaterial);
        return ResponseResult.okResult(fileServerUrl+s);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        if (dto == null) return errorParam;
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmMaterial::getUserId,ThreadLocalUtils.getUser());
        wrapper.eq(dto.getIsCollection()!=null,WmMaterial::getIsCollection,dto.getIsCollection());
        wrapper.orderByDesc(WmMaterial::getCreatedTime);
        return getPage(dto,wrapper);
    }

    @Override
    public ResponseResult delFile(Integer id) {
        if (id==null) return errorParam;
        WmMaterial wmMaterial = getById(id);
        if (wmMaterial==null)return imgNotExit;
        Integer count = wmnewsMaterialMapper.selectCount(Wrappers.<WmNewsMaterial>lambdaQuery().eq(WmNewsMaterial::getMaterialId, wmMaterial.getId()));
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"图片在被使用");
        }
        fastDFSClient.delFile(wmMaterial.getUrl());
        removeById(id);
        return successParam;
    }

    @Override
    public ResponseResult updateById(Integer id, Short collectMaterial) {
        if (id==null) return errorParam;
        WmMaterial wmMaterial = getById(id);
        if (wmMaterial==null)return imgNotExit;
        wmMaterial.setIsCollection(collectMaterial);
        updateById(wmMaterial);
        return successParam;
    }
}
